import router from './index'
import pinia from '@/store/store'
import { userInfoStore } from '../store/userStore'
const store = userInfoStore(pinia)

router.beforeEach((to, from, next) => {
  console.log('to',to)
  if (!store.token) {
    // 未登录  
    console.log('to', to)
    if (to.matched.length > 0 && !to.matched.some(record => record.meta.requiresAuth)) {
      next();
    } else {
      next({ path: "/login" })
    }
  } else {
    // 用户已经登录  
    if (store.token) {
      store.FETCH_PERMISSION().then(()=>{
        next();
      })
    } else {
      // store存在权限
      if (to.path !== "/login") {
        next();
      } else {
        next(from.fullPath)
      }
    }
  }
})